﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taurus.Platform.DTO;
using Taurus.Platform.BLL;
using ComponentFactory.Krypton.Toolkit;
using Taurus.Platform.XML;
using Taurus.Platform.Net;

namespace Taurus.Platform.UI
{
    public partial class MyFriendUI : UserControl
    {
        public UserAccount SelectFriend { get; set; }
        private UserAccount _myAccount;

        private UserHeadImage _headImage;
        private UserAccountPersistence _uap;
        private FriendBll _friendBll;
        private AccountBll _accountBll;
        private bool flag = true;//标志位判断MyAccount是否登录过

        public MyFriendUI(UserAccount myAccount)
        {
            _myAccount = myAccount;
            InitializeComponent();
            dgvFriend.AutoGenerateColumns = false;
            _headImage = new UserHeadImage();
            _uap = new UserAccountPersistence();
            _accountBll = new AccountBll();
            _friendBll = new FriendBll(myAccount);
        }

        private void MyFriendUI_Load(object sender, EventArgs e)
        {
            dgvFriend.DataSource = _myAccount.WrapperFriends;
        }

        private void dgvFriend_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            UserAccount friend = (UserAccount)dgvFriend.Rows[e.RowIndex].DataBoundItem;
            lblID.Values.ExtraText = friend.UID;
            lblName.Values.ExtraText = friend.Name;
            picHeadImage.Image = _headImage.GetImage(friend);

            //更新组时返回为<script language=javascript>new parent.dialog().reset();</script> 现在机制无法判断用户是否登录过
            if (flag)
            {
                Util.Authenticated(_myAccount);
                flag = false;
            }
            _friendBll.InitFriendGroup(friend);
            _accountBll.UpdateAccount(friend);

            lblGroup.Text = "用户所在组  ";
            foreach (var item in friend.Groups)
            {
                lblGroup.Text += item+"、";
            }
            lblGroup.Text = lblGroup.Text.Substring(0, lblGroup.Text.Length - 1);
            
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            if (dgvFriend.SelectedRows.Count!=0)
            {
                SelectFriend = (UserAccount)dgvFriend.SelectedRows[0].DataBoundItem;
            }            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            flag = false;

            List<UserAccount> friends = _friendBll.GetMyFriendsFromNet();
            foreach (var item in friends)
            {
                _accountBll.UpdateFriendAccount(item);
            }
            _accountBll.UpdateAccount(_myAccount);
            dgvFriend.DataSource = friends;
        }
    }
}
